package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class ClientPanel extends JPanel implements ActionListener
{
	private JPanel bigButtonP = null, mainP = null;
	private JButton addClientB = null, lookClientB = null,
					spareB = null;
	AddClientPanel acp;
	LookClientPanel lcp;
	Connection conn;
	JFrame frame;
	private int accessLevel = 1;
	DetailsPanel dp;
	AddressPanel ap1;
	
	public ClientPanel(JFrame frame, Connection conn)
	{
		this.frame = frame;
		this.conn = conn;
		this.setLayout(new BorderLayout());
		spareB = new JButton();
		bigButtonP = new JPanel(new GridLayout(1,2));
		addClientB = new JButton("Add New Client");
		addClientB.addActionListener(this);
		addClientB.setBorderPainted(false);
		addClientB.setBackground(new Color(215,215,215));
		addClientB.setFocusPainted(false);
		lookClientB = new JButton("Look Up Client");
		lookClientB.addActionListener(this);
		lookClientB.setBorderPainted(false);
		lookClientB.setBackground(new Color(238,238,238));
		lookClientB.setFocusPainted(false);
		bigButtonP.add(lookClientB);
		bigButtonP.add(addClientB);

		bigButtonP.setPreferredSize(new Dimension(780, 30));

		
		mainP = new JPanel(new GridLayout(1,1));
		lcp = new LookClientPanel(frame, conn);
		mainP.add(lcp);
		dp = lcp.getDetailsPanel();
		ap1 = lcp.getAddressPanel();
		
		this.add(bigButtonP, BorderLayout.NORTH);
		this.add(mainP, BorderLayout.CENTER);
	
	}
	public void emptyAll()
	{
		dp.clientIDT.setText("");
		dp.surnameT.setText("");
		dp.nameT.setText("");
		dp.initialsT.setText("");
		dp.idNumberT.setText("");
		dp.phoneOneT.setText("");
		dp.phoneTwoT.setText("");
		dp.emailT.setText("");
		dp.titleJCB.setSelectedItem("");
		dp.custStatusJCB.setSelectedItem("");
		dp.jdc.setDate(null);
		ap1.streetT.setText("");
		ap1.suburbT.setText("");
		ap1.cityT.setText("");
		ap1.posCodeT.setText("");
		//searchT.setText("");
	}
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == lookClientB)
		{
			LookClientPanel lcp = new LookClientPanel(frame, conn);
			addClientB.setBackground(new Color(215,215,215));
			lookClientB.setBackground(new Color(238,238,238));
			mainP.removeAll();
			mainP.add(lcp);
			this.validate();
			this.repaint();
		}
		if(source == addClientB)
		{
			acp = new AddClientPanel(frame, conn);
			lookClientB.setBackground(new Color(215,215,215));
			addClientB.setBackground(new Color(238,238,238));
			mainP.removeAll();
			mainP.add(acp);
			this.validate();
			this.repaint();
		}
	}
	public int getAccessLevel()
	{
		return accessLevel;
	}
	public static void main(String [] args)
	{
		new ClientTestFrame();
	}
}

class ClientTestFrame extends JFrame
{
	public ClientTestFrame()
	{	
		try
		{
			Connection conn;
			String userid, password;
			userid = "root";
			password = "";
			MySQL_Connection msc = new MySQL_Connection(
					"turtleData","localhost",userid, password);
			conn = msc.getMySQLConnection();
		
			ClientPanel cp = new ClientPanel(this, conn);
			this.setTitle("Employee frame");
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.getContentPane().add(cp, BorderLayout.CENTER);
			this.setSize(800,600);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
	}
}